package com.leyou.search.web;

import com.leyou.search.entity.Goods;
import com.leyou.search.entity.SearchParamDTO;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/init")
    public ResponseEntity<String> init(){
        //初始化索引库
        searchService.createIndexAndMapping();
        //加载数据(从数据库查询商品信息保存到elasticsearch)
        searchService.loadData();
        return ResponseEntity.ok("初始化索引库成功！");
    }

    //异步  根据关键字自动补全查询   返回补全后的词条列表
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String keyPrefix){
        return searchService.getSuggestion(keyPrefix);
    }

    //搜索商品列表
    //给用户提示信息后，接下来用户就会去搜索数据 跳转的页面
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoodsList(@RequestBody SearchParamDTO searchParamDTO){
        return searchService.searchGoodsList(searchParamDTO);
    }
}
